package com.rcdz.mykotlindemo.view.customview.ohter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.rcdz.mykotlindemo.tools.WjtTools;

import org.jetbrains.annotations.NotNull;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback, Handler.Callback {
    private final static String TAG = MySurfaceView.class.getName();
    SurfaceHolder surfaceHolder;
    Handler myHandler;
    Canvas canvas;
    private HandlerThread handlerThread;
    public MySurfaceView(Context context) {
        super(context);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        setFocusable(true);
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        myHandler = new Handler(handlerThread.getLooper(),this);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i("test","surfaceCreated");
        canvas = holder.lockCanvas();
        canvas.drawColor(Color.BLUE);
        holder.unlockCanvasAndPost(canvas);
        myHandler.sendEmptyMessage(1);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i("test","surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i("test","surfaceDestroyed");

    }

    @Override
    public boolean handleMessage(@NonNull @NotNull Message msg) {
        if(msg.what == 1){
            Log.d("test","收到消息了！！！");
        }
        return false;
    }
}
